<template>
  <div class="app-container">
     <!-- 模块标题与描述 -->
     <div class="section-header">
      <h2 class="title">飞行数据上传</h2>
      <p class="desc">请上传轨迹数据和传感器数据（JSON文件格式）</p>
    </div>


    <!-- 上传表单区域 -->
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="120px"
      class="upload-form"
    >
      <el-form-item label="学生id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入学生id" />
        </el-form-item>
      <el-form-item label="任务id" prop="taskId">
          <el-input v-model="form.taskId" placeholder="请输入任务id" />
      </el-form-item>
      <el-form-item label="飞行场景" prop="name">
          <el-select v-model="form.name" placeholder="请选择飞行场景" clearable filterable>
            <el-option label="森林巡检" value="森林巡检森林" />
            <el-option label="海域救援" value="海域救援海域" />
            <el-option label="雪山地形" value="雪山地形测绘" />
            <el-option label="城市监控" value="城市监控巡逻" />
          </el-select>
        </el-form-item>

      <el-form-item label="飞行时长（秒）" prop="duration">
        <el-input v-model="form.duration" placeholder="输入飞行时长" />
      </el-form-item>

      <el-form-item label="距离" prop="distance">
          <el-input v-model="form.distance" placeholder="请输入距离" />
        </el-form-item>

      <el-form-item label="轨迹数据文件" prop="record1">
        <el-upload
          v-model="form.record1"
          :auto-upload="false"
          :limit="1"
          
          class="upload-demo"
          action="#"
        >
          <el-button type="primary">选择文件</el-button>
          <div slot="tip" class="el-upload__tip">
            仅支持 JSON 格式文件，轨迹数据格式示例：<br />
            [
            <br />&nbsp;&nbsp;[x1, y1, z1],
            <br />&nbsp;&nbsp;[x2, y2, z2],
            <br />&nbsp;&nbsp;...
            <br />]
          </div>
        </el-upload>
      </el-form-item>

      <el-form-item label="传感器数据文件（可选）" prop="record2">
        <el-upload
          v-model="form.record2"
          :auto-upload="false"
          :limit="1"
          
          class="upload-demo"
          action="#"
        >
          <el-button type="primary">选择文件</el-button>
          <div slot="tip" class="el-upload__tip">
            可选 JSON 格式文件，传感器数据格式示例：<br />
            {
            <br />&nbsp;&nbsp;"altitude": [100, 101, 102, ...],
            <br />&nbsp;&nbsp;"speed": [10, 12, 15, ...],
            <br />&nbsp;&nbsp;"battery": [100, 99, 98, ...]
            <br />}
          </div>
        </el-upload>
      </el-form-item>

      

      <el-form-item >
        <el-button
          type="primary"
          @click="submitForm"  style="width: 90%"
        >
          上传飞行记录
        </el-button>
      </el-form-item>
    </el-form>

<hr>






    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="飞行场景" prop="name">
          <el-select v-model="form.name" placeholder="请选择飞行场景" clearable filterable>
            <el-option label="森林巡检" value="森林巡检森林" />
            <el-option label="海域救援" value="海域救援海域" />
            <el-option label="雪山测绘" value="雪山地形测绘" />
            <el-option label="城市监控" value="城市监控巡逻" />
          </el-select>
        </el-form-item>
      <el-form-item label="任务id" prop="taskId">
        <el-input
          v-model="queryParams.taskId"
          placeholder="请输入任务id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     
      <el-form-item label="学生id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入学生id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

   

    <el-table v-loading="loading" :data="flightsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学生id" align="center" prop="userId" />
      <el-table-column label="任务id" align="center" prop="taskId" />
      <el-table-column label="飞行场景" align="center" prop="name" />
      <el-table-column label="距离" align="center" prop="distance" />
      <el-table-column label="时长" align="center" prop="duration" />
      <el-table-column label="成绩" align="center" prop="score" />
      
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改飞行记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="轨迹数据">
          <fileUpload v-model="form.record1"/>
        </el-form-item>
        <el-form-item label="传感即数据">
          <fileUpload v-model="form.record2"/>
        </el-form-item>
        <el-form-item label="飞行场景" prop="name">
          <el-select v-model="form.name" placeholder="请选择飞行场景" clearable filterable>
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="距离" prop="distance">
          <el-input v-model="form.distance" placeholder="请输入距离" />
        </el-form-item>
        <el-form-item label="时长" prop="duration">
          <el-input v-model="form.duration" placeholder="请输入时长" />
        </el-form-item>
        <el-form-item label="任务id" prop="taskId">
          <el-input v-model="form.taskId" placeholder="请输入任务id" />
        </el-form-item>
        <el-form-item label="成绩" prop="score">
          <el-input v-model="form.score" placeholder="请输入成绩" />
        </el-form-item>
        <el-form-item label="学生id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入学生id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFlights, getFlights, delFlights, addFlights, updateFlights, exportFlights } from "../../../api/flights/flights/flights";
import FileUpload from '@/components/FileUpload';

export default {
  name: "Flights",
  components: {
    FileUpload,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 飞行记录表格数据
      flightsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        record1: null,
        record2: null,
        name: null,
        distance: null,
        duration: null,
        taskId: null,
        score: null,
        userId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询飞行记录列表 */
    getList() {
      this.loading = true;
      listFlights(this.queryParams).then(response => {
        this.flightsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        record1: null,
        record2: null,
        name: null,
        createTime: null,
        endTime: null,
        distance: null,
        duration: null,
        taskId: null,
        score: null,
        userId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加飞行记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFlights(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改飞行记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFlights(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFlights(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除飞行记录编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delFlights(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有飞行记录数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportFlights(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>


<style>
.desc {
  font-size: 14px;
  color: #999;
}
.el-upload__tip{
  font-size: 10px;
  color: #999;
  line-height: 13px;

}
</style>
